/// @Author lixiaobin
/// @Date 2025/1/14
/// @Description
import 'package:flutter/material.dart';
import 'package:leader_doctor_flutter/common/utils/log_utils.dart';

class LongPressContainer extends StatefulWidget {
  final Widget childView;
  final int duration;
  final Function function;

  const LongPressContainer({super.key, required this.childView, required this.duration, required this.function});

  @override
  _LongPressContainerState createState() => _LongPressContainerState();
}

class _LongPressContainerState extends State<LongPressContainer> {
  int _startTime = 0;
  int _endTime = 0;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onLongPressStart: (_) {
        _startTime = DateTime.now().millisecondsSinceEpoch;
      },
      onLongPressEnd: (_) {
        _endTime = DateTime.now().millisecondsSinceEpoch;
        _startLongPressTimer();
      },
      child: widget.childView,
    );
  }

  void _startLongPressTimer() {
    if ((_endTime - _startTime) / 1000 > widget.duration) {
      Log.i('长按事件触发');
      widget.function();
    }
  }
}
